import axios, { AxiosRequestConfig } from 'axios';


const http = axios.create({
  baseURL: "http://api-toutiao-web.itheima.net/mp/v1_0",
  timeout: 4000
})


http.interceptors.request.use((config: AxiosRequestConfig<any>) => {

  const token = window.localStorage.getItem('token');
  if (token) {
    config.headers!.Authorization = `Bearer ${token}`
  }


  return config
}, (err: any) => {
  return Promise.reject(err)
})



http.interceptors.response.use((config: any) => {
  return config.data
}, err => {
  if (err && err.response) {
    console.log(err);
  }
  return Promise.reject(err)
})

export default http